//
//  YJQSportGPSSignalButton.m
//  GoodLuck
//
//  Created by 杨瑞 on 17/2/15.
//  Copyright © 2017年 杨瑞. All rights reserved.
//

#import "YJQSportGPSSignalButton.h"
#import "YJQSportTypeModel.h"

@implementation YJQSportGPSSignalButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)awakeFromNib {
    [super awakeFromNib];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gpsChange:) name:YJQSportGPSSignalChangedNotification object:nil];
}

-(void)dealloc {
    //注销通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)gpsChange:(NSNotification *)notification {
    
    // 1. 从通知中获取 GPS 强度信号
    YJQSportGPSSignalState state = [notification.object integerValue];
    // 2. 根据 state 设置按钮的图像和标题
    NSString *imageName = (_isMapButton) ? @"ic_sport_gps_map_" : @"ic_sport_gps_";
    NSString *title;
    
    switch (state) {
        case YJQSportGPSSignalStateDisconnect:
            title = @"  GPS已经断开";
            imageName = [imageName stringByAppendingString:@"disconnect"];
            break;
        case YJQSportGPSSignalStateBad:
            title = @"  请绕开高楼大厦";
            imageName = [imageName stringByAppendingString:@"connect_1"];
            break;
        case YJQSportGPSSignalStateNormal:
            imageName = [imageName stringByAppendingString:@"connect_2"];
            break;
        case YJQSportGPSSignalStateGood:
            imageName = [imageName stringByAppendingString:@"connect_3"];
            break;
    }
    [self setTitle:title forState:UIControlStateNormal];
    [self setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    //根据是否存在标题设置右边内容的间距
    UIEdgeInsets inset = self.contentEdgeInsets;
    inset.right = (title == nil) ? 4 : 8;
    self.contentEdgeInsets = inset;
}
@end
